package com.xuecheng.content.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.CourseAuditDto;
import com.xuecheng.commons.model.dto.CourseBaseDto;
import com.xuecheng.commons.model.dto.CourseDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.vo.CourseBaseVo;
import com.xuecheng.commons.model.vo.CourseVo;
import com.xuecheng.commons.model.vo.PageResponseResult;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.web.exceptions.BusinessException;
import io.minio.errors.*;
import org.simpleframework.xml.Path;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * <p>
 * 课程基本信息前端控制器
 * </p>
 *
 * @author 七安
 */
@Slf4j
@RestController
@RequestMapping("/course")
public class CourseBaseController {

    @Autowired
    private CourseBaseService courseBaseService;

    /**
     * 分页查询课程
     *
     * @param courseDto   接收部分查询条件，例如名字，状态
     * @param pageRequest 分页数据
     * @return 课程列表
     */
    @PostMapping("/list")
    public PageResponseResult findClassList(@RequestBody CourseDto courseDto, PageRequest pageRequest) {
        //调用Service查询
        return courseBaseService.findClassList(courseDto, pageRequest);
    }

    /**
     * 课程封面上传
     *
     * @return 上传后的地址
     */
    @PostMapping("/upload")
    public ResponseResult<String> fileUpload(MultipartFile uploadFile) throws IOException, ServerException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        String fileUrl = courseBaseService.fileUpload(uploadFile);
        return ResponseResult.okResult(fileUrl);
    }

    /**
     * 添加课程
     *
     * @param courseBaseDto 课程信息
     * @return 添加完成后的id
     */
    @PostMapping
    public ResponseResult<CourseBaseVo> insertCourse(@RequestBody CourseBaseDto courseBaseDto) throws BusinessException {
        return ResponseResult.okResult(courseBaseService.insertCourse(courseBaseDto));
    }

    /**
     * 根据ID删除
     *
     * @param courseId 要删除的id
     * @return 删除结果
     * @throws BusinessException 删除失败
     */
    @DeleteMapping("/{courseId}")
    public ResponseResult deleteCourseById(@PathVariable("courseId") String courseId) throws BusinessException {
        courseBaseService.deleteCourseById(courseId);
        return ResponseResult.okResult();
    }

    /**
     * 根据ID查询课程
     *
     * @param courseId 课程id
     * @return 查询到的结果
     */
    @GetMapping("/{courseId}")
    public ResponseResult<CourseVo> findCourseById(@PathVariable String courseId) {
        CourseBase courseBase = courseBaseService.getById(courseId);
        //转成vo对象
        CourseVo courseVo = BeanHelper.copyProperties(courseBase, CourseVo.class);
        return ResponseResult.okResult(courseBase);
    }

    /**
     * 根据id修改课程
     *
     * @param courseBaseDto 要修改的内容
     * @return 课程id
     */
    @PutMapping
    public ResponseResult<CourseBaseVo> updateCourseById(@RequestBody CourseBaseDto courseBaseDto) throws BusinessException {
        Long id = courseBaseService.updateCourseById(courseBaseDto);
        //创建返回对象
        CourseBaseVo courseBaseVo = new CourseBaseVo();
        //设置id
        courseBaseVo.setCourseBaseId(id);
        //返回
        return ResponseResult.okResult(courseBaseVo);
    }

    /**
     * 提交审核
     *
     * @param courseBaseId 要提交的课程id
     * @return 结果
     */
    @GetMapping("/commit/{courseBaseId}")
    public ResponseResult commitVerify(@PathVariable("courseBaseId") Long courseBaseId) throws BusinessException {
        //调用Service
        courseBaseService.commitVerify(courseBaseId);
        return ResponseResult.okResult();
    }

    /**
     * 审核课程
     *
     * @param courseAuditDto 审核的意见和状态
     * @return 结果
     */
    @PostMapping("/approve")
    public ResponseResult verifyCourse(@RequestBody CourseAuditDto courseAuditDto) throws BusinessException {
        //调用service
        courseBaseService.verifyCourse(courseAuditDto);
        return ResponseResult.okResult();
    }

    /**
     * 课程下架
     *
     * @param courseId 课程id
     * @return 结果
     */
    @GetMapping("/offline/{courseId}")
    public ResponseResult courseSoldOut(@PathVariable("courseId") Long courseId) throws BusinessException, ServerException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return courseBaseService.courseSoldOut(courseId);
    }
}
